﻿/// <reference path="_namespace.js" />
/// <reference path="Vector3.js" />


/*****************************************************************************
Ajax library class definition. Kevin Robinson 2008.
Class: ajax.math.Ray3
/*****************************************************************************/

ajax.lang.addNamespace("ajax.math");
$imports("ajax.math.Vector3");

ajax.math.Ray3 = function(vectorOrigin, vectorDirection)
{
    this.normal = ajax.math.Vector3.normalize(vectorDirection);
    this.origin = vectorOrigin;
}

// Statics
ajax.math.Ray3.cast = function(obj)
{///<returns type="ajax.math.Ray3"/>
    return obj;
}

// Public properties
ajax.math.Ray3.prototype.normal = ajax.math.Vector3.prototype;
ajax.math.Ray3.prototype.origin = ajax.math.Vector3.prototype;

// public methods
ajax.math.Ray3.distanceToPlane = function(ray, vectorPlaneNormal, floatPlaneDistance)
{ ///<param name="ray" type="ajax.math.Ray3"/>
  ///<param name="vectorPlaneNormal" type="ajax.math.Vector3"/>
  ///<param name="floatPlaneDistance" type="float"/>
  ///<returns type="float"/>

    // Get the cosign of the angle between the direction of the ray and the normal of the plane
    var cosAlpha = ajax.math.Vector3.dot(ray.normal, vectorPlaneNormal);
    // Protect
    if (cosAlpha == 0)
        cosAlpha = 0.00001;
    
    var fDeltaD = floatPlaneDistance - ajax.math.Vector3.dot(ray.origin, vectorPlaneNormal);
    return fDeltaD / cosAlpha;
}

ajax.math.Ray3.distanceToPlaneFromFace = function(ray, face)
{///<param name="ray" type="ajax.math.Ray3"/>
 ///<param name="face" type="ajax.drawing.Face"/>
 ///<returns type="float"/>  
    return ajax.math.Ray3.distanceToPlane(ray, face.normal, face.getPlaneDistance());
}

ajax.math.Ray3.distanceToPlaneFromPlane = function(ray, plane)
{///<param name="ray" type="ajax.math.Ray3"/>
 ///<param name="plane" type="ajax.math.Plane3"/>
 ///<returns type="float"/>
    return ajax.math.Ray3.distanceToPlane(ray, plane.normal, plane.distance);
}

ajax.math.Ray3.prototype.distanceToPlaneFromFace = function(face)
{///<param name="face" type="ajax.drawing.Face"/>
 ///<returns type="float"/>
    return ajax.math.Ray3.distanceToPlaneFromFace(this, face);
}

ajax.math.Ray3.prototype.distanceToPlaneFromPlane = function(plane)
{///<param name="plane" type="ajax.math.Plane3"/>
///<returns type="float"/>
    return ajax.math.Ray3.distanceToPlaneFromPlane(this, plane);
}

ajax.math.Ray3.prototype.distanceToPlane = function(vectorPlaneNormal, floatPlaneDistance)
{///<param name="vectorPlaneNormal" type="ajax.math.Vector3"/>
 ///<param name="floatPlaneDistance" type="float"/>
 ///<returns type="float"/>
    return ajax.math.Ray3.distanceToPlane(this, vectorPlaneNormal, floatPlaneDistance);
}

ajax.math.Ray3.prototype.toString = function()
{
    return String.toFormat("Position:{0} Direction:{1}", this.origin, this.normal);  
}